﻿<%@ WebHandler Language="C#" Class="xinxi_zidian" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class xinxi_zidian : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["ziDianType"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("请重试...");
                return;
            }
            else
            {
                switch (requestType)
                {
                    case "1":
                        string fenZu = string.Format("{0}", context.Request.Form["fenZu"]);
                        string createCondition = string.Empty;
                        if (!string.IsNullOrEmpty(fenZu))
                        {
                            createCondition = m_DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, fenZu, "", SearchConditionType.等于);
                        }
                        Dictionary<string, string> orderBys = new Dictionary<string, string>();
                        orderBys.Add("fenzu", "asc");
                        orderBys.Add("shijian", "asc");
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_zidian", createCondition, orderBys);
                        string jsonStr = JsonConvert.SerializeObject(Dt);
                        context.Response.Write(jsonStr);
                        break;
                    case "2":
                        string ziDianID = string.Format("{0}", context.Request.Form["ziDianID"]);
                        string fenZuMingCheng = string.Format("{0}", context.Request.Form["fenzu"]);
                        string xiangMuMingCheng = string.Format("{0}", context.Request.Form["xiangmu"]);
                        if (string.IsNullOrEmpty(ziDianID))
                        {
                            //新增
                            TableRow newRow = new TableRow("tbl_zidian", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol(); col1.ColName = "fenzu"; col1.ValueMember = fenZuMingCheng; col1.ValueType = DataValueCommonType.字符串;
                            TableCol col2 = new TableCol(); col2.ColName = "xiangmu"; col2.ValueMember = xiangMuMingCheng; col2.ValueType = DataValueCommonType.字符串;
                            TableCol col3 = new TableCol(); col3.ColName = "shijian"; col3.ValueMember = DateTime.Now; col3.ValueType = DataValueCommonType.日期时间;
                            TableCol col4 = new TableCol(); col4.ColName = "shangchuanzhuangtai"; col4.ValueMember = "1"; col4.ValueType = DataValueCommonType.字符串;
                            TableCol col5 = new TableCol(); col5.ColName = "zhandian"; col5.ValueMember = "云端"; col5.ValueType = DataValueCommonType.字符串;
                            TableCol col6 = new TableCol(); col6.ColName = "kehubianhao"; col6.ValueMember = ConfigurationManager.AppSettings["客户编号"]; col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            newRow.DataCols.Add(col2);
                            newRow.DataCols.Add(col3);
                            newRow.DataCols.Add(col4);
                            newRow.DataCols.Add(col5);
                            newRow.DataCols.Add(col6);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        else
                        {
                            //修改 
                            TableRow newRow = new TableRow("tbl_zidian", ziDianID, DBCtl.修改);
                            TableCol col1 = new TableCol(); col1.ColName = "fenzu"; col1.ValueMember = fenZuMingCheng; col1.ValueType = DataValueCommonType.字符串;
                            TableCol col2 = new TableCol(); col2.ColName = "xiangmu"; col2.ValueMember = xiangMuMingCheng; col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            newRow.DataCols.Add(col2);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        break;
                    case "3":
                        string delZiDianID = context.Request.Form["ziDianID"];
                        if (string.IsNullOrEmpty(delZiDianID))
                        {
                            context.Response.Write("没有找到数据，请重试...");
                            return;
                        }
                        TableRow delRow = new TableRow();
                        delRow.TableName = "tbl_zidian";
                        delRow.ID = delZiDianID;
                        if (m_DB.DeleteRowByID(delRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("Failed");
                        }
                        break;
                    case "4":
                        string currFenZu = string.Format("{0}", context.Request.Form["fenZu"]);
                        string searchCondition = m_DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, currFenZu, "", SearchConditionType.等于);
                        Dictionary<string, string> leftTables = new Dictionary<string, string>();
                        leftTables.Add("tbl_xinghao", "tbl_xinghao.shuiwenliao=tbl_zidian.xiangmu");
                        Dictionary<string, string> currOrderBys = new Dictionary<string, string>();
                        currOrderBys.Add("fenzu", "asc");
                        currOrderBys.Add("shijian", "asc");
                        DataTable currDt = m_DB.GetDataTableByCondition("tbl_zidian", "xiangmu,xinghao,tbl_xinghao.id", leftTables, searchCondition, "", currOrderBys);
                        for (int i = 0; i < currDt.Rows.Count; i++)
                        {
                            DataRow row = currDt.Rows[i];
                            string rowXingHao = string.Format("{0}", row["xinghao"]);
                            if (!string.IsNullOrEmpty(rowXingHao))
                            {
                                row["xinghao"] = string.Format("{0}({1})", row["xiangmu"], row["xinghao"]);
                            }
                            else
                            {
                                if (row["xiangmu"].Equals("水"))
                                {
                                    row["id"] = "shui";
                                    row["xinghao"] = string.Format("{0}", row["xiangmu"]);
                                }
                                else
                                {
                                        currDt.Rows.Remove(row);
                                        i--;
                                }

                            }
                        }

                        string currJson = JsonConvert.SerializeObject(currDt);
                        context.Response.Write(currJson);
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("字典信息异常。{0}", err.Message));
            context.Response.Write("请重试...");
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}